﻿using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using IGM.DataProvider;
using IGM.Framework.Constants;

namespace IGM.Business
{
    public partial class Business
    {
        #region -- Get Subcommittee --

        /// <summary>
        /// This function shows list of Student.
        /// </summary>
        /// <returns></returns>
        public List<Subcommittee> GetSubcommitteeByKindInternId(int kindinternId, int majorId)
        {
            if (DataContex == null || kindinternId <= 0) return null;

            return DataContex.Subcommittees.Where(m => m.IdKindIntern == kindinternId && m.IdMajor == majorId).ToList();
        }

        #endregion

        #region Common

        private static bool SubcommitteeCommonValidator(Subcommittee subcommittee, out string message)
        {
            if (string.IsNullOrWhiteSpace(subcommittee.Name))
            {
                message = string.Format(ConstantMessages.RequireFieldIsBlank, "tên Tiểu ban");
                return false;
            }

            if (subcommittee.Name.Length >= 50)
            {
                message = string.Format(ConstantMessages.FieldDataOverMaxLength, "tên Tiểu ban", 50);
                return false;
            }

            if (subcommittee.NumberMembers < 0)
            {
                message = string.Format(ConstantMessages.DataUnderMinValue, "số lượng thành viên");
                return false;
            }
            message = string.Empty;
            return true;
        }

        #endregion

        #region -- Add Subcommittee --

        /// <summary>
        /// 
        /// </summary>
        /// <param name="subcommittee"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool AddSubcommitteeValidator(Subcommittee subcommittee, out string message)
        {
            if (!SubcommitteeCommonValidator(subcommittee, out message)) return false;

            if (DataContex.Subcommittees.Any(f => f.Name.ToLower() == subcommittee.Name.ToLower()))
            {
                message = string.Format(ConstantMessages.ItemIsExisted, subcommittee.Name);
                return false;
            }

            return true;
        }

        /// <summary>
        /// This function add a new facutly into list.
        /// </summary>
        public void AddSubcommittee(Subcommittee subcommittee)
        {
            DataContex.Subcommittees.AddObject(subcommittee);
            DataContex.SaveChanges();
        }

        public void AddSubcommittee(List<Subcommittee> subcommittees)
        {
            foreach (var subcommittee in subcommittees)
                AddSubcommittee(subcommittee);

            MessageBox.Show("OK1");
        }
        #endregion

        #region -- Edit Subcommittee --


        public bool EditSubcommitteeValidator(Subcommittee subcommittee, out string message)
        {
            var id = subcommittee.Id;

            if (!SubcommitteeCommonValidator(subcommittee, out message)) return false;

            if (DataContex.Subcommittees.Any(m => m.Name.ToLower() == subcommittee.Name.ToLower() && m.Id != id))
            {
                message = string.Format(ConstantMessages.ItemIsExisted, subcommittee.Name);
                return false;
            }

            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="subcommittee"></param>
        /// <returns></returns>
        public bool EditSubcommittee(Subcommittee subcommittee)
        {
            var id = subcommittee.Id;
            var updateSubcommittee = DataContex.Subcommittees.SingleOrDefault(f => f.Id == id);

            if (updateSubcommittee == null)
                return false;

            updateSubcommittee.Name = subcommittee.Name;
            updateSubcommittee.IdKindIntern = subcommittee.IdKindIntern;
            updateSubcommittee.NumberMembers = subcommittee.NumberMembers;
            updateSubcommittee.IdMajor = subcommittee.IdMajor;

            DataContex.SaveChanges();
            return true;
        }

        #endregion

        #region -- Delete Subcommittee --

        public bool DeleteSubcommitteesValidator(IEnumerable<Subcommittee> subcommittees, out string message)
        {
            const string intialize = "Tiểu ban: ";
            var itemsCannotIdentify = (from subcommittee1 in subcommittees
                                       let id = subcommittee1.Id
                                       where !DataContex.Subcommittees.Any(m => m.Id == id)
                                       select subcommittee1).Aggregate(intialize, (current, subcommittee1) => current + (subcommittee1.Name + ", "));

            if (itemsCannotIdentify != intialize)
            {
                message = string.Format(ConstantMessages.SomeItemsCanNotIdentify, itemsCannotIdentify.TrimEnd(','));
                return false;
            }

            var itemsInUse = (from subcommittee1 in subcommittees
                              let id = subcommittee1.Id
                              where DataContex.SubcommitteeGroups.Any(m => m.IdSubcommittee == id) || DataContex.SubcommitteeLecturers.Any(n => n.IdSubcommittee == id
                              || DataContex.SubcommitteeStudents.Any(s => s.IdSubcommittee == id))
                              select subcommittee1).Aggregate(intialize, (current, subcommittee1) => current + (subcommittee1.Name + ", "));

            if (itemsInUse != intialize)
            {
                message = string.Format(ConstantMessages.SomeItemsInUseCanNotDelete, itemsInUse.TrimEnd(','));
                return false;
            }

            message = string.Empty;
            return true;
        }

        /// <summary>
        /// Delete object
        /// </summary>
        public void DeleteSubcommittees(List<Subcommittee> subcommittees)
        {
            if (subcommittees == null)
                return;

            for (var i = subcommittees.Count - 1; i >= 0; i--)
            {
                var id = subcommittees[i].Id;
                

                // Delete Subcommitttee Student
                var deleteSStudent = DataContex.SubcommitteeStudents.Where(m => m.IdSubcommittee == id).ToList();
                for(var index = deleteSStudent.Count - 1; index >=0; index--)
                {
                    var idSubcommitteeStudent = deleteSStudent[index].Id;
                    var deleteSubcommitteeStudent = DataContex.SubcommitteeStudents.Single(m => m.Id == idSubcommitteeStudent);
                    DataContex.SubcommitteeStudents.DeleteObject(deleteSubcommitteeStudent);
                }

                // Delet Subcommitttee Lecturer
                var deleteSLecturer = DataContex.SubcommitteeLecturers.Where(m => m.IdSubcommittee == id).ToList();
                for (var index = deleteSLecturer.Count - 1; index >= 0; index--)
                {
                    var idSubcommitteeLecturers = deleteSLecturer[index].Id;
                    var deleteSubcommitteeLecturer = DataContex.SubcommitteeLecturers.Single(m => m.Id == idSubcommitteeLecturers);
                    DataContex.SubcommitteeLecturers.DeleteObject(deleteSubcommitteeLecturer);
                }         

                // Delete Subcommittee Group
                var deleteGSubcommittee = DataContex.SubcommitteeGroups.Where(m => m.IdSubcommittee == id).ToList();
                for (var index = deleteGSubcommittee.Count - 1; index >= 0; index--)
                {
                    var idSubcommitteeGroup = deleteGSubcommittee[index].Id;
                    var deleteSubcommitteeGroup = DataContex.SubcommitteeGroups.Single(m => m.Id == idSubcommitteeGroup);
                    DataContex.SubcommitteeGroups.DeleteObject(deleteSubcommitteeGroup);           
                }

                var deleteSubcommittee = DataContex.Subcommittees.Single(m => m.Id == id);
                DataContex.Subcommittees.DeleteObject(deleteSubcommittee);
            }
            DataContex.SaveChanges();
        }

        public void ClearData(int kindinterId)
        {
            var deleteSubcommittees = DataContex.Subcommittees.Where(m => m.IdKindIntern == kindinterId).ToList();

            DeleteSubcommittees(deleteSubcommittees);
        }

        #endregion
       
    }
}